/* ----------------------------------------------------------------------
 * File: 		main.cpp
 * Created by:		Taylor Wood
 * Email:		Tdwood2@asu.edu
 * Student ID:		1202553801
 *
 * Description:
 * This is the main file. Here we will open the input file
 * and create an output file based on the argv parameters.
 * This file also serves to read strings from the input file
 * and call isOperator, isDelimeter, and LEXER which are 
 * defined in lexer.cpp.
 * ----------------------------------------------------------------------*/

#include <iostream> 	// This is for testing purposes , I will remove this later.
#include <fstream>	// This is for reading and writing files
#include <string>	// Need them strings
#include "lexer.h"

bool bInStr = false;

int main(int argc, char *argv[]) {

	std::ifstream fin;	// to read from files
	std::ofstream fout;	// to write to files
	std::string line = "";	// string variable to store the read line
	std::string word = "";	// string variable to store the split line

	// open the file and check to see if it exists
	fin.open(argv[1]);	// argv[1] contains the input file
	fout.open(argv[2]);	// argv[2] contains the output file

	// if the file does not exist
	if (!fin) {
		std::cout << "The file \"" << argv[1] << "\" does not exist. Please provide another file.\n";
		return -1;
	}

	// File exists, begin reading from the file
	while (std::getline(fin, line)) {
	
		
		// navigate through the read line
		for (int i = 0; i < line.length() - 1; i++) {

				// Check to see if the letter is an operator, delimiter, or space
				if ((isOperator(line[i]) || isDelimiter(line[i]) || line[i] == ' ') || line[i] == '\"' || line[i] == '\'') {
					
					// first check if we have a double or single quotes
					if (line[i] == '\"') {
						// if something is in the word pass to the lexer
						if (!word.empty()) {
							fout << lexer(word) << "\t" << word << std::endl;
							word = "";		// clear the word		
						}
						// continue building the string
						word += line[i];
						do {
							i++;
							word += line[i];
						
						} while (i < line.length() && line[i] != '\"');
					}
					// first check if we have a double or single quotes
					if (line[i] == '\'') {
						// if something is in the word pass to the lexer
						if (!word.empty()) {
							fout << lexer(word) << "\t" << word << std::endl;
							word = "";		// clear the word		
						}
						// continue building the string
						word += line[i];
						do {
							i++;
							word += line[i];
						
						} while (i < line.length() && line[i] != '\'');
					}
					

					// Now that we know there is a character which splits the
					// string, lets check if our existing object is a word.
					if (word.length() > 0) {
						fout << lexer(word) << "\t" << word << std::endl;
						word = "";
					}
				
					// check to see if line[i] is an operator or delimiter
					if (isDelimiter(line[i]) || isOperator(line[i])) {
						word = line[i];
						fout << lexer(word) << "\t" << word << std::endl;
						word = "";
					}
					
				}
				// If if it not true, start creating a word
				else
					word += line[i];
			//}
		}
		// There may be the interesting case where a text file does not end with
		// a space, delimiter, or operator. Therefor, let's check to see what
		// is in the word	
		if (word.length() > 0) {
			fout << lexer(word) << "\t" << word << std::endl;
			word = "";
		}

	}

	return 0;

}
